public class test {
    //leetcode 202.快乐数(1-双指针检测环焦点)
    public boolean isHappy(int n) {
        int slow = n;
        int fast = bitSum(n);
        while(slow != fast){
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }
        return slow == 1;
    }
    public int bitSum(int n){
        int num = 0;
        while(n > 0){
            int count = (n % 10) * (n % 10);
            num += count;
            n /= 10;
        }
        return num;
    }
    //leetcode 202.快乐数(2-数学)
    class Solution {
        public boolean isHappy(int n) {
            int tmp = n;
            while(n / 10 > 0){
                int num = 0;
                while(tmp > 0){
                    int count = (tmp % 10) * (tmp % 10);
                    num += count;
                    tmp /= 10;
                }
                n = num;
                tmp = n;
            }
            return (n == 1 || n == 7);
        }
    }
    //leetcode 13.罗马数字转整数
    class Solution {
        public int romanToInt(String s) {
            int num = 0;
            int len = s.length();
            for(int i = 0;i < len;i++){
                char c = s.charAt(i);
                switch(c){
                    case 'I':{
                        if(i != len - 1 && s.charAt(i + 1) == 'V'){
                            i++;
                            num += 4;
                        } else if(i != len - 1 && s.charAt(i + 1) == 'X'){
                            i++;
                            num += 9;
                        }else {
                            num += 1;
                        }
                    } break;
                    case 'V': num += 5; break;
                    case 'X': {
                        if(i != len - 1 && s.charAt(i + 1) == 'L'){
                            i++;
                            num += 40;
                        } else if(i != len - 1 && s.charAt(i + 1) == 'C'){
                            i++;
                            num += 90;
                        }else {
                            num += 10;
                        }
                    } break;
                    case 'L': num += 50; break;
                    case 'C': {
                        if(i != len - 1 && s.charAt(i + 1) == 'D'){
                            i++;
                            num += 400;
                        } else if(i != len - 1 && s.charAt(i + 1) == 'M'){
                            i++;
                            num += 900;
                        }else {
                            num += 100;
                        }
                    } break;
                    case 'D': num += 500; break;
                    case 'M': num += 1000; break;
                }
            }
            return num;
        }
    //leetcode 374.猜数字大小(二分查找)
    public class Solution extends GuessGame {
        public int guessNumber(int n) {
            int left = 1;
            int right = n;
            int mid = left;
            while(left < right){
                mid = left + (right - left) / 2;
                if(guess(mid) == -1){
                    right = mid;
                }else if(guess(mid) == 1){
                    left = mid + 1;
                } else {
                    return mid;
                }
            }
            return left;
        }
    }
}
